﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Platform.Hardware.ControlCard.Base
{
    public interface IControlCard
    {
        /// <summary>
        /// 表示是否初始化
        /// </summary>
        bool Initialized { get; }
        /// <summary>
        /// 连接状态
        /// </summary>
        bool Connected { get; }
        /// <summary>
        /// 运行消息
        /// </summary>
        string Message { get; }
        /// <summary>
        /// 消息观察者
        /// </summary>
        IObservable<string> MessageObservable { get; }
        /// <summary>
        /// 初始化控制卡
        /// </summary>
        /// <param name="controlCardConfig"></param>
        /// <returns></returns>
        bool Init(ControlCardConfig controlCardConfig);
        /// <summary>
        /// 关闭控制卡
        /// </summary>
        void Close();

        #region 控制卡IO点，轴位置和状态 

        IOEntity IOEntity { get; }

        float XAxisPostion { get; }
        float YAxisPostion { get; }
        float ZAxisPostion { get; }
        float LeftAxisPostion { get; }
        float RightAxisPostion { get; }
        float KnifeAxisPostion { get; }
        float MainAxisPostion { get; }
        float TrackAxisPostion { get; }
        float StyletAxisPostion { get; }
        int LeftAxisEnable { get; }
        int RightAxisEnable { get; }

        #endregion

        //定义方法成员

        /// <summary>
        /// 设置轴运行速度 
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="speed"></param>
        /// <returns></returns>
        bool SetSpeed(AxisType axis, float speed);
        /// <summary>
        /// 设置轴的脉冲当量
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        bool SetUnit(AxisType axis, float unit);
        /// <summary>
        /// 轴回原点（同步,可以取消）
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        bool MoveToZero(AxisType axis, CancellationToken? cancellationToken = null);
        /// <summary>
        /// 轴回原点（异步,可以取消）
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        Task<bool> MoveToZeroAsync(AxisType axis, CancellationToken? cancellationToken = null);
        /// <summary>
        /// 轴相对运动(同步,可取消)
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        bool MoveRelative(AxisType axis, float distance, CancellationToken? cancellationToken = null);
        /// <summary>
        /// 轴相对运动(异步,可取消)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> MoveRelativeAsync(AxisType axis, float distance, CancellationToken? cancellationToken = null);
        /// <summary>
        /// 轴绝对运动(同步，可取消)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="distance"></param>
        /// <returns></returns>
        bool MoveAbsolute(AxisType axis, float distance, CancellationToken? cancellationToken = null);
        /// <summary>
        /// 轴绝对运动(异步，可取消)
        /// </summary>
        /// <param name="axis1"></param>
        /// <param name="distance"></param>
        /// <returns></returns>
        Task<bool> MoveAbsoluteAsync(AxisType axis, float distance, CancellationToken? cancellationToken = null);
        /// <summary>
        /// 轴连续运行（异步）
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        Task<bool> MoveContinueAsync(AxisType axis, MoveDirection direction);
        /// <summary>
        /// 停止所有轴
        /// </summary>
        /// <param name="stopMode"></param>
        /// <returns></returns>
        bool StopAxis(StopMode stopMode = StopMode.立即中断脉冲发送);
        /// <summary>
        /// 轴停止
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="stopMode"></param>
        /// <returns></returns>
        bool StopAxis(AxisType axis, StopMode stopMode = StopMode.取消当前和缓冲运动);
        /// <summary>
        /// 轴停止(异步)
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="stopMode"></param>
        /// <returns></returns>
        Task<bool> StopAxisAsync(AxisType axis, StopMode stopMode = StopMode.取消当前和缓冲运动);
        /// <summary>
        /// 获取轴位置
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        bool GetAxisPosition(AxisType axis, ref float position, ref int runState);
        /// <summary>
        /// 轴暂停
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        bool AxisPause(AxisType axis, int mode);
        /// <summary>
        /// 轴恢复
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        bool AxisResume(AxisType axis);
        /// <summary>
        /// 轴使能
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        bool AxisEnable(AxisType axis);
        /// <summary>
        /// 轴使能
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="isEnable"></param>
        /// <returns></returns>
        bool AxisEnable(AxisType axis, bool isEnable);
        /// <summary>
        /// 获取输入点值
        /// </summary>
        /// <param name="io"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        bool GetSingleInput(IOType io, ref uint value);
        /// <summary>
        /// 设置输出点
        /// </summary>
        /// <param name="io"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        bool SetSingleOutput(IOType io, uint value);
        bool GetMultiInput(int start, int end, ref int[] value);
        bool GetMultiOutput(ushort start, ushort end, ref uint[] value);
    }
}
